package me.passtime.projt.fee;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import me.passtime.projt.Home;
import me.passtime.projt.Main;
import me.passtime.projt.R;
import me.passtime.projt.query.QueryCurrentMonthPage;
import me.passtime.projt.query.QueryHistoryPage;
import me.passtime.projt.util.AlertDialogUtils;
import me.passtime.projt.util.PageJumpUtils;
import me.passtime.projt.util.SystemUtils;
import me.passtime.projt.util.UserHolder;

/**
 * Created by IntelliJ IDEA.
 * User: Lee
 * Date: 11-12-6
 * Time: 下午1:26
 */
public class PaymentSuccessPage extends Activity implements View.OnClickListener {
    private double amount = 0;
    private String phoneNumber;
    private Button mUserInfo;
    private TextView mHasAmount;
    private TextView mPayAmount;
    private TextView mPhoneNumber;

    private Handler mHandler;
    private ProgressDialog mDialog;
    private static final int DIALOG_PROGRESS = 19;
    private PaymentQueryController mController;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.payment_success);
        mController=new PaymentQueryController(this);
        Bundle extras = getIntent().getExtras();
        amount = extras.getDouble("amount");
        phoneNumber = extras.getString("phoneNumber");

        mUserInfo = (Button) this.findViewById(R.id.payment_success_person_btn);
        mUserInfo.setText(UserHolder.phoneNumber + "\r\n" + UserHolder.userName);
        mHasAmount = (TextView) this.findViewById(R.id.payment_success_has_amount);
        mPayAmount = (TextView) this.findViewById(R.id.payment_success_amount);

        mPayAmount.setText(amount + "");

        mPhoneNumber = (TextView) this.findViewById(R.id.payment_success_phone_number);
        mPhoneNumber.setText(phoneNumber);

        this.findViewById(R.id.payment_success_month_btn).setOnClickListener(this);
        this.findViewById(R.id.payment_success_record_btn).setOnClickListener(this);

        this.findViewById(R.id.payment_confirm_back_btn).setOnClickListener(this);
        this.findViewById(R.id.payment_confirm_logout_btn).setOnClickListener(this);
        this.findViewById(R.id.payment_success_my_account_btn).setOnClickListener(this);
        this.findViewById(R.id.payment_success_fee_records_btn).setOnClickListener(this);
        this.findViewById(R.id.payment_success_exit).setOnClickListener(this);

        mHandler = new Handler();
        queryCanUseAmount();

    }

    public void updateAmount(final String amount){
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                mHasAmount.setText(amount);
            }
        });
    }

    private void queryCanUseAmount() {
        showDialog(DIALOG_PROGRESS);
        mController.queryPayment(UserHolder.phoneNumber);
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.payment_success_month_btn:
                gotoQueryCurrentMonthPage();
                break;
            case R.id.payment_success_record_btn:
                gotoQueryHistoryPage();
                break;
            case R.id.payment_confirm_back_btn:
                PageJumpUtils.jump(this, Home.class);
                break;
            case R.id.payment_confirm_logout_btn:
                PageJumpUtils.logoutAndJumpToMainPage(this);
                break;
            case R.id.payment_success_my_account_btn:
                PageJumpUtils.jumpToManageAccountPage(this);
                break;
            case R.id.payment_success_fee_records_btn:
                PageJumpUtils.jumpToQueryPaymentPage(this);
                break;
            case R.id.payment_success_exit:
                AlertDialogUtils.showYesNoDiaLog(this, "是否退出系统?", new AlertDialogUtils.Executor() {
                    @Override
                    public void execute() {
                        SystemUtils.killSelf();
                        finish();
                    }
                });
                break;
        }
    }

    @Override
    public void onBackPressed() {
        PageJumpUtils.jump(this, Home.class);
    }

    private void gotoQueryHistoryPage() {
        PageJumpUtils.jump(this, QueryHistoryPage.class);
    }

    private void gotoQueryCurrentMonthPage() {
        PageJumpUtils.jump(this, QueryCurrentMonthPage.class);
    }

    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
            case DIALOG_PROGRESS:
                mDialog = new ProgressDialog(this);
                mDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
                mDialog.setMessage("正在处理，请等待......");
                return mDialog;
            default:
                return null;
        }
    }

    public void hideProgressDialog() {
        if (mDialog != null) {
            removeDialog(DIALOG_PROGRESS);
        }
    }
}
